<template>
	<view class="addres-manage">
		<scroll-view scroll-y="true" >
			<view class="addres-manage-detail" v-for="item in addrList" :key="item.id">
				<view class="user-msg">
					<text>{{item.name}}</text>
					<text>{{item.phone}}</text>
				</view>
				<view class="user-address">
					{{item.address}}
				</view>
				<view>
					<view @click="editDefault(item.id,item.isDefault)" style="display:flex;align-items: center;">
						<image :src="address_img" v-if="item.isDefault == '1'"></image>
						<view class="noSelect" v-else></view>
						<text>设置为默认地址</text>
					</view>
					<view class="edit" @click="editAddress(item)">
						<image :src="edit_icon"></image>
						<text>编辑</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="add-address" @click="addAddress">
			新增地址
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				token: this.$store.getters.getToken,
				addrList:[],
				userId: this.$store.getters.getUserId,
				address_img: this.$imgUrl + 'img/member/address_img.png',
				edit_icon: this.$imgUrl + 'img/member/edit_icon.png',
				is_lucky: 0, // 是否小幸运
				lucky_id: 0
			}
		},
		onLoad:function(option){
			console.log('optionoptionoptionoptiondddddeeeeeeee', option)
			if (option.lucky_id) {
				this.is_lucky = 1
				this.lucky_id = option.lucky_id
			}
		},
		onShow() {
			this.getAddr()
		},
		methods:{
			getAddr(){
				let data ={
					token:this.token,
					userID:this.userId
				}
				this.$api.getAddrList(data).then(res => {
					console.log('用户地址',res)
					if(res.status){
						this.addrList = res.list
						var reg = /^(\d{3})\d{4}(\d{4})$/
						for(let i = 0;i < this.addrList.length; i++){
							this.addrList[i].phone = this.addrList[i].mobile.replace(reg, "$1****$2")
						}
					}
				}).catch(err => {
					console.log('用户地址',err)
				})
			},
			addAddress(){
				uni.navigateTo({
					url:'./addAddress/addAddress?page=add'
				})
			},
			editAddress(item){
				uni.navigateTo({
					url:'./addAddress/addAddress?page=edit&item=' + JSON.stringify(item)
				})
			},
			editDefault(id,isDefault){
				if(isDefault==1) {
					uni.showToast({
						title: '该地址已经是默认地址',
						icon: 'none',
						duration: 1500
					})
				}else {
					console.log('this.is_lucky', this.is_lucky)
					if (this.is_lucky) {
						var updateData = {
							token:this.token,
							id:id,
							userID: this.userId,
							isDefault: 1,
							lucky_id: this.lucky_id
						}
					} else {
						var updateData = {
							token:this.token,
							id:id,
							userID: this.userId,
							isDefault: 1,
						}
					}
					this.$api.editAddrDefault(updateData).then(res => {
						console.log(res)
						if(res.status){
							uni.showToast({
								title: '修改成功',
								icon: 'none',
								duration: 1000
							})
							setTimeout(()=> {
								this.getAddr()
							},1000)
						}else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
							setTimeout(()=> {
								uni.navigateBack({})
							},1000)
						}
					}).catch(err => {
						console.log('修改默认地址',err)
					})
				}
			}
		}
	}
</script>

<style>
	page{
		background: #F5F6F9;
	}
	.addres-manage{
		width: 100%;
		height: 100%;
		padding-top: 20upx;
		box-sizing: border-box;
	}
	.addres-manage>view:first-child{
		margin-top: 0;
	}
	.addres-manage-detail{
		width: 100%;
		/* height: 210upx; */
		margin-top: 20upx;
		background: #FFFFFF;
		padding: 0 30upx;		
		padding-top: 22upx;
		box-sizing: border-box;
	}
	.user-msg{
		color: #333333;
	}
	.user-msg>text{
		display: inline-block;
	}
	.user-msg>text:first-child{
		font-size: 30upx;
		font-weight: 550;
	}
	.user-msg>text:last-child{
		font-size: 28upx;
		margin-left: 30upx;
	}
	.user-address{
		margin-top: 8upx;
		color: #666666;
		font-size: 28upx;
		padding-bottom: 22upx;
		box-sizing: border-box;
		border-bottom: 1upx solid #E6E6E6;
	}
	.addres-manage-detail>view:last-child{
		display: flex;
		justify-content: space-between;
		padding: 18upx 0;
		box-sizing: border-box;
	}
	image{
		width: 28upx;
		height: 28upx;
		/* vertical-align: middle; */
	}
	.noSelect {
		width: 26upx;
		height: 26upx;
		border-radius: 50%;
		border: 2upx solid #DEDEDE;
	}
	.addres-manage-detail>view:last-child>view:first-child>text{
		display: inline-block;
		margin-left: 20upx;
		color: #333333;
		font-size: 24upx;
	}
	.edit {
		display: flex;
		align-items: center;
	}
	.addres-manage-detail>view:last-child .edit>text{
		display: inline-block;
		margin-left: 10upx;
		color: #666666;
		font-size: 24upx;
	}
	.add-address{
		margin: 0 auto;
		margin-top: 192upx;
		width: 638upx;
		line-height: 88upx;
		line-height: 88upx;
		text-align: center;
		color: #FFFFFF;
		background: linear-gradient(#CBEC00,#00A66A);
		box-shadow: 0 8upx 16upx 0 rgba(0,166,106,0.4);
		border-radius: 44upx;
		font-size: 32upx;
	}
</style>
